//
//  JDSendPrivateMessageViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/6/3.
//

import UIKit
import ProgressHUD
// 发送私聊
class JDSendPrivateMessageViewController: JDBaseViewController {

    var remoteName: String?
    var remoteId: String?
    var recordId: String?
    
    var bottomContainerView: UIView?
    var cancelButton: UIButton?
    var sendButton: UIButton?
    
    var containerView: UIView?
    var inputTitle: UILabel?
    var inputContentView: UITextView?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupConfig()
        self.setupUIElements()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.setupNavigationTitle(self.remoteName ?? "")
        self.setupLeftBackItem()
        self.view.backgroundColor = self.commonColor
    }

    func setupUIElements() {
        self.bottomContainerView = UIView()
        self.view.addSubview(self.bottomContainerView!)
        self.bottomContainerView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
            make.height.equalTo(104)
        })
        self.bottomContainerView?.backgroundColor = .white

        self.cancelButton = UIButton(type: .custom)
        self.bottomContainerView?.addSubview(self.cancelButton!)
        self.cancelButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.bottomContainerView!.snp.centerX).offset(-5)
            make.centerY.equalTo(self.bottomContainerView!)
            make.left.equalTo(self.bottomContainerView!).offset(12)
            make.height.equalTo(54)
        })
        self.cancelButton?.layer.cornerRadius = 27
        self.cancelButton?.backgroundColor = color("#F5F6F8")
        self.cancelButton?.setTitle("返回", for: .normal)
        self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.cancelButton?.setTitleColor(.black, for: .normal)
        self.cancelButton?.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        
        self.sendButton = UIButton(type: .custom)
        self.bottomContainerView?.addSubview(self.sendButton!)
        self.sendButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.bottomContainerView!.snp.centerX).offset(5)
            make.right.equalTo(self.bottomContainerView!).offset(-12)
            make.height.equalTo(54)
            make.centerY.equalTo(self.bottomContainerView!)
        })
        self.sendButton?.setTitle("推送", for: .normal)
        self.sendButton?.backgroundColor = color("#FA6161")
        self.sendButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.sendButton?.setTitleColor(.white, for: .normal)
        self.sendButton?.layer.cornerRadius = 27
        self.sendButton?.addTarget(self, action: #selector(sendPrivateMessageAction), for: .touchUpInside)
        
//        var containerView: UIView?
//        var inputTitle: UILabel?
//        var inputContentView: UITextView?
        self.containerView = UIView()
        self.view.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints({ make in
            make.left.equalTo(self.view).offset(12)
            make.top.equalTo(self.view).offset(14+statusBarHeight+44)
            make.right.equalTo(self.view).offset(-12)
        })
        self.containerView?.layer.cornerRadius = 8
        self.containerView?.backgroundColor = .white
        
        self.inputTitle = UILabel()
        self.containerView?.addSubview(self.inputTitle!)
        self.inputTitle?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.containerView!).offset(12)
        })
        self.inputTitle?.text = "私聊内容"
        self.inputTitle?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 15, weight: .semibold), textColor: color("#333333"))
        
        self.inputContentView = UITextView()
        self.containerView?.addSubview(self.inputContentView!)
        self.inputContentView?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.inputTitle!.snp.bottom).offset(10)
            make.right.equalTo(self.containerView!).offset(-12)
            make.height.greaterThanOrEqualTo(107)
            make.bottom.equalTo(self.containerView!).offset(-10)
        })
        self.inputContentView?.placeholder = "请输入私聊内容"
        self.inputContentView?.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        self.inputContentView?.textColor = color("#333333")
        
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

    @objc func sendPrivateMessageAction() {
        if (self.inputContentView?.text.count ?? 0) <= 0 {
            self.view.toast(message: "请输入私聊内容")
            return
        }
        
        var params = [String: Any]()
        params["receiveUserId"] = self.remoteId
        params["content"] = self.inputContentView?.text
        params["dragonBusinessDistrictInfoId"] = self.recordId
        ProgressHUD.animate()
        JDNetWorkRequest(.sendDragonUnionChatMessage(params)) { responseStr in
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "推送私聊成功")
                self.navigationController?.popToRootViewController(animated: true)
            }
//            self.dismissLoading()
            ProgressHUD.dismiss()
        } failure: { error in
            self.view.toast(message: error.localizedDescription)
//            self.dismissLoading()
            ProgressHUD.dismiss()
        } error: { errorCode in
            self.view.toast(message: "网络错误")
//            self.dismissLoading()
            ProgressHUD.dismiss()
        }

        
//        printLog("发送私信")
//        var params = [String: Any]()
//        params["jsuserid"] = self.remoteId
//
//        params["userid"] = JDLoginModel.default.userId
//        params["context"] = self.inputContentView?.text
//
//        JDNetWorkRequest(.sendMessage(params)) { responseStr in
//
//            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
//            if commonModel?.code == "0" {
//
//            }
//
//            printLog("发送成功:\(responseStr)")
//            self.backAction()
//
//        } failure: { error in
//            printLog("发送错误\(error)")
//
//        } error: { errorCode in
//            printLog("发送错误:\(errorCode)")
//        }

        
        
        
    }
    
}
